package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类品牌中间表 业务实现类
 *
 * @author atguigu
 * @since 2023-04-18
 */
@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Autowired
    private BaseTrademarkService baseTrademarkService;

    /**
     * 查询当前分类关联的品牌列表
     *
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getTrademarkByCategoryId(Long category3Id) {
        //1.根据分类ID查询 分类品牌关系表 获取当前分类关联所有品牌ID集合
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = this.list(queryWrapper);
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {

            //1.1 从分类品牌集合中获取品牌ID集合
            List<Long> tradeMarkIdList = baseCategoryTrademarkList.stream()
                    .map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());


            //2.根据品牌ID集合查询品牌集合
            List<BaseTrademark> baseTrademarkList = baseTrademarkService.listByIds(tradeMarkIdList);
            return baseTrademarkList;
        }
        return null;
    }

    /**
     * 查询未关联到当前分类品牌列表
     *
     * @param category3Id 三级分类ID
     * @return
     */
    @Override
    public List<BaseTrademark> getCurrentTrademarkList(Long category3Id) {
        //1.方式1:在业务代码中 两次单表查询(分别查询品牌所有;再查询中间表获取当前分类下所有品牌ID) 采用Stream流进行过滤

        //2.采用: 方式2:采用自定义SQL实现查询 (子查询,关联查询)
        BaseTrademarkMapper trademarkBaseMapper = (BaseTrademarkMapper) baseTrademarkService.getBaseMapper();
        List<BaseTrademark> baseTrademarkList = trademarkBaseMapper.getCurrentTrademarkList(category3Id);
        return baseTrademarkList;
    }

    @Override
    public void saveBaseCategoryTrademark(CategoryTrademarkVo categoryTrademarkVo) {
        //1.获取关联到当前分类品牌Id集合 转为 分类品牌关系对象集合
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();

        if (!CollectionUtils.isEmpty(trademarkIdList)) {
            List<BaseCategoryTrademark> categoryTrademarkList = trademarkIdList.stream().map(trademarkId -> {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(trademarkId);
                return baseCategoryTrademark;
            }).collect(Collectors.toList());

            //2.批量执行保存分类品牌集合
            this.saveBatch(categoryTrademarkList);
        }

    }

    /**
     * 删除指定分类下关联品牌
     *
     * @param category3Id
     * @param trademarkId
     * @return
     */
    @Override
    public void deleteCategoryTrademark(Long category3Id, Long trademarkId) {
        //1.构建逻辑删除条件
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        queryWrapper.eq(BaseCategoryTrademark::getTrademarkId, trademarkId);

        //2.执行删除
        this.remove(queryWrapper);
    }
}
